package com.robot.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.robot.vo.ResponseMessage;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/10/27
 * Time: 16:30
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Component
public class AssertExpressionService {

    public ResponseMessage check(Integer assertType, String expectResult, String expressionList) {
        JSONArray jsonArray = JSON.parseArray(expressionList); //list类型
        Iterator iterator = jsonArray.iterator();
        List list = Lists.newArrayList();
        switch (assertType) {
            case 4:
                //jsonPath断言类型
                while (iterator.hasNext()) {
                    //jsonObject ==map
                    JSONObject jsonObject = (JSONObject) iterator.next();
                    String expression = jsonObject.getString("value");
                    try {
                        Object result = JsonPath.read(expectResult, expression);
                        list.add(result);
                    } catch (Exception e) {
                        return ResponseMessage.errorResponse("当前的表达式:"+expression+"异常提示:"+e.getMessage());
                    }
                }
                break;
            case 5:
                //正则表达式
                break;
            default:

        }
        return ResponseMessage.successResponse(list);
    }
}
